package com.power.sifa.respond.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.respond.api.IApl_secondoriFeign;
import com.power.sifa.respond.model.Apl_secondori;
import com.power.sifa.respond.model.pageInfo.Apl_secondoriPageInfo;
import com.power.sifa.respond.service.Apl_secondoriService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 *   经过二审的原审生效审理信息控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_secondoriController",tags={"经过二审的原审生效审理信息"})
public class Apl_secondoriController implements IApl_secondoriFeign {

    @Resource
    private Apl_secondoriService apl_secondoriService;


    @Override
    @ApiOperation("查询经过二审的原审生效审理信息分页列表")
    @SaCheckPermission("apl_secondori_list")
    public ResponseDTO list(Apl_secondoriPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_secondori> page = this.apl_secondoriService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_secondori apl_secondori : page) {
                apl_secondori.geneText();
                list.add(apl_secondori);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看经过二审的原审生效审理信息")
    @SaCheckPermission("apl_secondori_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_secondori apl_secondori  = apl_secondoriService.getById(id);
        if (apl_secondori != null) {
            apl_secondori.geneText();
        }
        return ResponseDTO.succData(apl_secondori);
    }

    @Override
    @ApiOperation("删除经过二审的原审生效审理信息")
    @SaCheckPermission("apl_secondori_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_secondoriService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改经过二审的原审生效审理信息")
    @SaCheckPermission("apl_secondori_save")
    public ResponseDTO save(Apl_secondori apl_secondori) {
        Assert.notNull(apl_secondori, "apl_secondori不能为空");
        apl_secondoriService.saveOrUpdate(apl_secondori);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除经过二审的原审生效审理信息")
    @SaCheckPermission("apl_secondori_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_secondoriService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_secondori_list")
    public void output(HttpServletResponse response, Apl_secondoriPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "经过二审的原审生效审理信息";
        String[] heads = {"序号","一审法院","一审判决日期","一审审理结果","一审案件编号","负责人是否出庭","经过二审的原审出庭类型","法院类型","一审判决结果"};
        String[] fields = {"id","court","judgdate","result","code","iscourt","type","courttype","firstres"};
        Page<Apl_secondori> page = this.apl_secondoriService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "经过二审的原审生效审理信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","一审法院","一审判决日期","一审审理结果","一审案件编号","负责人是否出庭","经过二审的原审出庭类型","法院类型","一审判决结果"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","court","judgdate","result","code","iscourt","type","courttype","firstres"};
        List<Apl_secondori> list = (List<Apl_secondori>)ExcelUtil.Import(fields, file, Apl_secondori.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_secondoriService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
